/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/

function BVToolbar()
{
	var fClick = $createDelegate(this, this.click);
	this.m_bBackwardDisabled = true;
	this.m_bForwardDisabled = true;
	this.m_aImages = new Object();
	this.m_bRecord = true;

	var aTemp = new Array("back", "forward", "go", "home", "refresh", "stop");

	for (var i = 0 ;
		 i < aTemp.length;
		 ++i)
	{
		var sName = aTemp[i];
		this.m_aImages[sName + "_normal"] = new Image();
		this.m_aImages[sName + "_normal"].src = "./images/toolbar/" + sName + "_24_normal.gif";
		this.m_aImages[sName + "_hot"] = new Image();
		this.m_aImages[sName + "_hot"].src = "./images/toolbar/" + sName + "_24_hot.gif";
		this.m_aImages[sName + "_disabled"] = new Image();
		this.m_aImages[sName + "_disabled"].src = "./images/toolbar/" + sName + "_24_disabled.gif";
	}

	this.m_aImages.record = new Image();
	this.m_aImages.record.src = "./images/toolbar/record_24_normal.gif";
	this.m_aImages.pause = new Image();
	this.m_aImages.pause.src = "./images/toolbar/pause_24_normal.gif";

	$get("txtToolbarAddress").attachEvent("onkeypress",
										  $createDelegate(this, this.ToolbarAddressKeyPress));

	$get("btnToolbarBack").attachEvent("onclick", fClick);
	$get("btnToolbarForward").attachEvent("onclick", fClick);
	$get("btnToolbarStop").attachEvent("onclick", fClick);
	$get("btnToolbarRefresh").attachEvent("onclick", fClick);
	$get("btnToolbarHome").attachEvent("onclick", fClick);
	$get("btnToolbarRecord").attachEvent("onclick", fClick);
	$get("btnToolbarOK").attachEvent("onclick",
									 $createDelegate(this, this.go));

// 	this.MouseOver = function(elemSrc)
// 	{
// 		var imgTarget = document.getElementById(elemSrc.id.replace("btn", "img"));
// 		switch (elemSrc.id)
// 		{
// 			case "btnToolbarBack":
// 			case "imgToolbarBack":
// 				if (false == this.bBackwardDisabled)
// 				{
// 					imgTarget.src = me.m_aImages["back_hot"].src;
// 				}
// 				break;

// 			case "btnToolbarForward":
// 			case "imgToolbarForward":
// 				if (false == this.bForwardDisabled)
// 				{
// 					imgTarget.src = me.m_aImages["forward_hot"].src;
// 				}
// 				break;

// 			case "btnToolbarStop":
// 			case "imgToolbarStop":
// 				imgTarget.src = me.m_aImages["stop_hot"].src;
// 				break;

// 			case "btnToolbarRefresh":
// 			case "imgToolbarRefresh":
// 				imgTarget.src = me.m_aImages["refresh_hot"].src;
// 				break;
				
// 			case "btnToolbarHome":
// 			case "imgToolbarHome":
// 				imgTarget.src = me.m_aImages["home_hot"].src;
// 				break;
			
// 			case "btnToolbarOK":
// 			case "imgToolbarOK":
// 				// imgTarget.src = this.aImages["go_hot"].src;
// 				imgTarget = me.m_aImages["go_hot"];
// 				break;

// 		}		
// 		return;
// 	}


// 	this.MouseOut = function(elemSrc)
// 	{
// 		var imgTarget = document.getElementById(elemSrc.id.replace("btn", "img"));
// 		switch (elemSrc.id)
// 		{
// 			case "btnToolbarBack":
// 			case "imgToolbarBack":
// 				if (false == this.bBackwardDisabled)
// 				{
// 					imgTarget.src = me.m_aImages["back_normal"].src;
// 				}
// 				break;

// 			case "btnToolbarForward":
// 			case "imgToolbarForward":
// 				if (false == this.bForwardDisabled)
// 				{
// 					imgTarget.src = me.m_aImages["forward_normal"].src;
// 				}
// 				break;

// 			case "btnToolbarStop":
// 			case "ImgToolbarStop":
// 				imgTarget.src = me.m_aImages["stop_normal"].src;
// 				break;

// 			case "btnToolbarRefresh":
// 			case "imgToolbarRefresh":
// 				imgTarget.src = me.m_aImages["refresh_normal"].src;
// 				break;
				
// 			case "btnToolbarHome":
// 			case "imgToolbarHome":
// 				imgTarget.src = me.m_aImages["home_normal"].src;
// 				break;

// 			case "btnToolbarOK":
// 			case "imgToolbarOK":
// 				imgTarget.src = me.m_aImages["go_normal"].src;
// 				break;
// 		}
// 		return;
// 	}
		
		

}


BVToolbar.prototype.click = function()
{
	var objBrowser = window.objActiveBrowser;
	switch (window.event.srcElement.id)
	{
		case "btnToolbarBack":
			objBrowser.GoBack();
			window.oAppEvents.Back();
			break;
			
		case "btnToolbarForward":
			objBrowser.GoForward();
			window.oAppEvents.Forward();
			break;

		case "btnToolbarStop":
			objBrowser.Stop();
			break;
			
		case "btnToolbarRefresh":
			// http://msdn.microsoft.com/workshop/browser/webbrowser/reference/enums/refreshconstants.asp
			// REFRESH_NORMAL = 0
			// Refresh that does not include sending the HTTP "pragma:nocache" header to the server.
			// REFRESH_IFEXPIRED = 1
			// Refresh that occurs if the page has expired.
			// REFRESH_COMPLETELY = 2
			// Refresh that includes sending a "pragma:nocache" header to the server (HTTP URLs only).
			objBrowser.Refresh2(0);
			break;
			
		case "btnToolbarHome":
			objBrowser.GoHome();
			window.oAppEvents.Home();
			break;

		case "btnToolbarRecord":
			this.m_bRecord = !this.m_bRecord;
			if (true == this.m_bRecord)
			{
				$get("imgToolbarRecord").src = this.m_aImages.pause.src;
				window.oAppEvents.record();
			}
			else
			{
				$get("imgToolbarRecord").src = this.m_aImages.record.src;
				window.oAppEvents.pause();
			}
			break;
			
	}

};


BVToolbar.prototype.go = function()
{
	var objBrowser = window.objActiveBrowser;
	var sUrl = $get("txtToolbarAddress").value;
	objBrowser.Navigate2(sUrl);
	window.oAppEvents.AddNavigate(sUrl);
};
									 



BVToolbar.prototype.ToolbarAddressKeyPress = function()
{
	var nKeyCode = window.event.keyCode;

	if (13 == nKeyCode)
	{
		this.go();
	}

	window.event.cancelBubble = true;

};


BVToolbar.prototype.CommandStateChange = function(nCommands, bEnabled)
{
	var elemButton = null;
	if (1 == nCommands)
	{
		elemButton = $get("btnToolbarForward");
		this.m_bForwardDisabled = (0 == bEnabled) ? true : false;
		$get("btnToolbarForward").disabled = this.m_bForwardDisabled;
		$get("imgToolbarForward").src = (false == this.m_bForwardDisabled) ? 
			this.m_aImages["forward_normal"].src :
			this.m_aImages["forward_disabled"].src;
	}
	else if (2 == nCommands)
	{
		elemButton = $get("btnToolbarBack");
		this.m_bBackwardDisabled = (0 == bEnabled) ? true : false;
		$get("btnToolbarBack").disabled = this.m_bBackwardDisabled;
		$get("imgToolbarBack").src = (false == this.m_bBackwardDisabled) ?	this.m_aImages["back_normal"].src :	this.m_aImages["back_disabled"].src;
	}
	return;
};




